package com.highcom.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
public class Handler {

    // http://localhost:8080/goto
    @GetMapping("/goto")
    public String goTo(HttpServletRequest request){
        //request.setAttribute("msg","成功了...");
        request.setAttribute("code",200);
        return "forward:/success";
    }

    @GetMapping("/params")
    public String testParams(Map<String,Object> map,
                             Model model,
                             HttpServletRequest request,
                             HttpServletResponse response){
        map.put("hello","666");
        model.addAttribute("world","hello666");
        request.setAttribute("message","ok");

        return "forward:/success";
    }


    @ResponseBody
    @GetMapping("/success")
    public Map success(@RequestAttribute(value = "msg",required = false)String msg
                      ,@RequestAttribute(value = "code",required = false)String code,
                       HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        map.put("msg",msg);
        map.put("code",code);
        Object hello = request.getAttribute("hello");
        Object world = request.getAttribute("world");
        Object message = request.getAttribute("message");
        map.put("hello",hello);
        map.put("world",world);
        map.put("message",message);

        return map;
    }

}
